﻿
using Microsoft.AspNetCore.DataProtection;

using SRM.Core.Models.Base;

using System.Collections.Generic;

namespace SRM.Core.Utility
{
    public class DataProtector
    {
        private readonly IDataProtector _protector;

        public DataProtector(IDataProtectionProvider dataProtectionProvider
            , DataProtectionPurposeStrings strings)
        {
            _protector = dataProtectionProvider.CreateProtector(strings.Constant);
        }

        public List<int> DataDecryption(string idStr)
        {
            var strIds = idStr.Split(',');
            List<string> strId = new List<string>(strIds);
            List<int> Ids = new List<int>();
            foreach (var item in strId)
            {
                string decryptedId = _protector.Unprotect(item);
                int.TryParse(decryptedId, out int Id);
                Ids.Add(Id);
            }
            return Ids;
        }

        public int DataDecryptionSign(string ciphertext)
        {
            string decryptedId = _protector.Unprotect(ciphertext);
            int.TryParse(decryptedId, out int Id);
            return Id;
        }


        public string DataEncryption(int Id)
        {
            return _protector.Protect(Id.ToString());
        }
    }
}
